<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveSupport::Cache::MemCacheStore</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Class</span> 
            ActiveSupport::Cache::MemCacheStore 
            
                <span class="parent">&lt; 
                    
                    <a href="Store.html">ActiveSupport::Cache::Store</a>
                    
                </span>
            
        </h1>
        <ul class="files">
            
            <li><a href="../../../files/activesupport/lib/active_support/cache/mem_cache_store_rb.html">activesupport/lib/active_support/cache/mem_cache_store.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p>A cache store implementation which stores data in Memcached: <a
href="http://memcached.org/">memcached.org/</a></p>

<p>This is currently the most popular cache store for production websites.</p>

<p>Special features:</p>
<ul><li>
<p>Clustering and load balancing. One can specify multiple memcached servers,
and <a href="MemCacheStore.html">MemCacheStore</a> will load balance
between all available servers. If a server goes down, then <a
href="MemCacheStore.html">MemCacheStore</a> will ignore it until it comes
back up.</p>
</li></ul>

<p><a href="MemCacheStore.html">MemCacheStore</a> implements the <a
href="Strategy/LocalCache.html">Strategy::LocalCache</a> strategy which
implements an in-memory cache inside of a block.</p>

    </div>
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>B</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="MemCacheStore.html#method-c-build_mem_cache">build_mem_cache</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>C</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="MemCacheStore.html#method-i-clear">clear</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>N</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="MemCacheStore.html#method-c-new">new</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>R</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="MemCacheStore.html#method-i-read_multi">read_multi</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>S</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="MemCacheStore.html#method-i-stats">stats</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    
      <!-- Section constants -->
      <div class="sectiontitle">Constants</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class="attr-name">ESCAPE_KEY_CHARS</td>
            <td>=</td>
            <td class="attr-value">/[\x00-\x20%\x7F-\xFF]/n</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
      </table>
    


    


    <!-- Methods -->
    
      <div class="sectiontitle">Class Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-c-build_mem_cache">
            
              <b>build_mem_cache</b>(*addresses)
            
            <a href="MemCacheStore.html#method-c-build_mem_cache" name="method-c-build_mem_cache" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-build_mem_cache_source')" id="l_method-c-build_mem_cache_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/9ec14c223b787d04b381511799b9b21499e29e68/activesupport/lib/active_support/cache/mem_cache_store.rb#L29" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-build_mem_cache_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/cache/mem_cache_store.rb, line 29</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">build_mem_cache</span>(*<span class="ruby-identifier">addresses</span>)
  <span class="ruby-identifier">addresses</span> = <span class="ruby-identifier">addresses</span>.<span class="ruby-identifier">flatten</span>
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">addresses</span>.<span class="ruby-identifier">extract_options!</span>
  <span class="ruby-identifier">addresses</span> = [<span class="ruby-string">&quot;localhost:11211&quot;</span>] <span class="ruby-keyword">if</span> <span class="ruby-identifier">addresses</span>.<span class="ruby-identifier">empty?</span>
  <span class="ruby-constant">Dalli</span><span class="ruby-operator">::</span><span class="ruby-constant">Client</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">addresses</span>, <span class="ruby-identifier">options</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-c-new">
            
              <b>new</b>(*addresses)
            
            <a href="MemCacheStore.html#method-c-new" name="method-c-new" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Creates a new <a href="MemCacheStore.html">MemCacheStore</a> object, with
the given memcached server addresses. Each address is either a host name,
or a host-with-port string in the form of “host_name:port”. For example:</p>

<pre>ActiveSupport::Cache::MemCacheStore.new(&quot;localhost&quot;, &quot;server-downstairs.localnetwork:8229&quot;)</pre>

<p>If no addresses are specified, then <a
href="MemCacheStore.html">MemCacheStore</a> will connect to localhost port
11211 (the default memcached port).</p>

<p>Instead of addresses one can pass in a MemCache-like object. For example:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'memcached'</span> <span class="ruby-comment"># gem install memcached; uses C bindings to libmemcached</span>
<span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Cache</span><span class="ruby-operator">::</span><span class="ruby-constant">MemCacheStore</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">Memcached</span><span class="ruby-operator">::</span><span class="ruby-constant">Rails</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;localhost:11211&quot;</span>))
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-new_source')" id="l_method-c-new_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/9ec14c223b787d04b381511799b9b21499e29e68/activesupport/lib/active_support/cache/mem_cache_store.rb#L49" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-new_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/cache/mem_cache_store.rb, line 49</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">initialize</span>(*<span class="ruby-identifier">addresses</span>)
  <span class="ruby-identifier">addresses</span> = <span class="ruby-identifier">addresses</span>.<span class="ruby-identifier">flatten</span>
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">addresses</span>.<span class="ruby-identifier">extract_options!</span>
  <span class="ruby-keyword">super</span>(<span class="ruby-identifier">options</span>)

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">addresses</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:get</span>)
    <span class="ruby-ivar">@data</span> = <span class="ruby-identifier">addresses</span>.<span class="ruby-identifier">first</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">mem_cache_options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">dup</span>
    <span class="ruby-constant">UNIVERSAL_OPTIONS</span>.<span class="ruby-identifier">each</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">name</span><span class="ruby-operator">|</span> <span class="ruby-identifier">mem_cache_options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">name</span>)}
    <span class="ruby-ivar">@data</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">build_mem_cache</span>(*(<span class="ruby-identifier">addresses</span> <span class="ruby-operator">+</span> [<span class="ruby-identifier">mem_cache_options</span>]))
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">extend</span> <span class="ruby-constant">Strategy</span><span class="ruby-operator">::</span><span class="ruby-constant">LocalCache</span>
  <span class="ruby-identifier">extend</span> <span class="ruby-constant">LocalCacheWithRaw</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                  
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-clear">
            
              <b>clear</b>(options = nil)
            
            <a href="MemCacheStore.html#method-i-clear" name="method-i-clear" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Clear the entire cache on all memcached servers. This method should be used
with care when shared cache is being used.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-clear_source')" id="l_method-i-clear_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/9ec14c223b787d04b381511799b9b21499e29e68/activesupport/lib/active_support/cache/mem_cache_store.rb#L111" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-clear_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/cache/mem_cache_store.rb, line 111</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">clear</span>(<span class="ruby-identifier">options</span> = <span class="ruby-keyword">nil</span>)
  <span class="ruby-ivar">@data</span>.<span class="ruby-identifier">flush_all</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">Dalli</span><span class="ruby-operator">::</span><span class="ruby-constant">DalliError</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
  <span class="ruby-identifier">logger</span>.<span class="ruby-identifier">error</span>(<span class="ruby-node">&quot;DalliError (#{e}): #{e.message}&quot;</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">logger</span>
  <span class="ruby-keyword">nil</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-read_multi">
            
              <b>read_multi</b>(*names)
            
            <a href="MemCacheStore.html#method-i-read_multi" name="method-i-read_multi" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Reads multiple values from the cache using a single call to the servers for
all keys. Options can be passed in the last argument.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-read_multi_source')" id="l_method-i-read_multi_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/9ec14c223b787d04b381511799b9b21499e29e68/activesupport/lib/active_support/cache/mem_cache_store.rb#L68" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-read_multi_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/cache/mem_cache_store.rb, line 68</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">read_multi</span>(*<span class="ruby-identifier">names</span>)
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">names</span>.<span class="ruby-identifier">extract_options!</span>
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">merged_options</span>(<span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">keys_to_names</span> = <span class="ruby-constant">Hash</span>[<span class="ruby-identifier">names</span>.<span class="ruby-identifier">map</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">name</span><span class="ruby-operator">|</span> [<span class="ruby-identifier">escape_key</span>(<span class="ruby-identifier">namespaced_key</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span>)), <span class="ruby-identifier">name</span>]}]
  <span class="ruby-identifier">raw_values</span> = <span class="ruby-ivar">@data</span>.<span class="ruby-identifier">get_multi</span>(<span class="ruby-identifier">keys_to_names</span>.<span class="ruby-identifier">keys</span>, <span class="ruby-value">:raw</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">true</span>)
  <span class="ruby-identifier">values</span> = {}
  <span class="ruby-identifier">raw_values</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">entry</span> = <span class="ruby-identifier">deserialize_entry</span>(<span class="ruby-identifier">value</span>)
    <span class="ruby-identifier">values</span>[<span class="ruby-identifier">keys_to_names</span>[<span class="ruby-identifier">key</span>]] = <span class="ruby-identifier">entry</span>.<span class="ruby-identifier">value</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">entry</span>.<span class="ruby-identifier">expired?</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">values</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-stats">
            
              <b>stats</b>()
            
            <a href="MemCacheStore.html#method-i-stats" name="method-i-stats" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Get the statistics from the memcached servers.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-stats_source')" id="l_method-i-stats_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/9ec14c223b787d04b381511799b9b21499e29e68/activesupport/lib/active_support/cache/mem_cache_store.rb#L119" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-stats_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/cache/mem_cache_store.rb, line 119</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">stats</span>
  <span class="ruby-ivar">@data</span>.<span class="ruby-identifier">stats</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    